/*
 * @lc app=leetcode.cn id=209 lang=golang
 *
 * [209] 长度最小的子数组
 */

// @lc code=start

const MAXINT = 1<<31 - 1

func minSubArrayLen(s int, nums []int) int {
	i, j := 0, 0
	if len(nums) == 0 {
		return 0
	}
	m := MAXINT
	sum := 0
	for ; j < len(nums); j++ {
		sum += nums[j]
		for sum >= s {
			m = min(m, j-i+1)
			sum -= nums[i]
			i++
		}
	}

	if m == MAXINT {
		return 0
	} else {
		return m
	}
}

func min(a, b int) int {
	if a < b {
		return a
	} else {
		return b
	}
}

// @lc code=end

